<?php

use Hyperf\Database\Schema\Schema;
use Hyperf\Database\Schema\Blueprint;
use Hyperf\Database\Migrations\Migration;

class CreatePermissionRoleMenuTables extends Migration
{
    /**
     * Run the migrations.
     */
    public function up(): void
    {
        Schema::create('menu', function (Blueprint $table){
            $table->bigIncrements('id');
            $table->unsignedBigInteger('parent_id')->default(0)->comment('父级ID');
            $table->string('name', 50)->comment('菜单名称');
            $table->string('app', 20)->nullable()->comment('应用');
            $table->string('icon', 50)->nullable()->comment('图标');
            $table->string('path', 50)->nullable()->comment('路径');
            $table->unsignedTinyInteger('level')->comment('级别');
            $table->unsignedSmallInteger('sort')->default(0)->comment('排序');
            $table->string('desc')->nullable()->comment('描述');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态|["禁用","启用"]');
            $table->timestampsTz();
            $table->index(['parent_id','name']);
            $table->comment('菜单表');
        });
        Schema::create('permission', function (Blueprint $table) {
            $table->bigIncrements('id');
            $table->string('name', 50)->comment('权限名称');
            $table->unsignedBigInteger('menu_id')->default(0)->comment('菜单ID');
            $table->string('path', 100)->comment('权限路径');
            $table->string('link', 100)->nullable()->comment('前端路由');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态|["禁用","启用"]');
            $table->string('method', 50)->nullable()->comment('请求方法');
            $table->string('version', 20)->nullable()->comment('版本');
            $table->timestampsTz();
            $table->comment('权限表');
            $table->foreign('menu_id')->references('id')->on('menu');
        });
        Schema::create('role', function (Blueprint $table){
            $table->bigIncrements('id');
            $table->unsignedBigInteger('app_id')->nullable()->comment('应用ID');
            $table->string('name', 50)->comment('角色名称');
            $table->string('desc')->nullable()->comment('描述');
            $table->unsignedTinyInteger('status')->default(1)->comment('状态|["禁用","启用"]');
            $table->unsignedSmallInteger('sort')->default(0)->comment('排序');
            $table->string('app')->comment('应用');
            $table->timestampsTz();
            $table->comment('角色');
        });
        Schema::create('admin_log', function (Blueprint $table){
            $table->bigIncrements('id');
            $table->unsignedBigInteger('out_id')->nullable()->comment('外键');
            $table->string('model')->nullable()->comment('模型');
            $table->unsignedBigInteger('admin_id')->comment('用户ID');
            $table->string('app', 50)->default('admin')->comment('应用');
            $table->unsignedInteger('ip')->nullable()->comment('IP');
            $table->text('user_agent')->nullable()->comment('用户代理');
            $table->unsignedTinyInteger('type')->comment('操作类型|{"1":"查看","2":"新增","3":"编辑","4":"导出","5":"登录","6":"修改密码","7":"删除","8":"注册"}');
            $table->text('desc')->nullable()->comment('详情');
            $table->string('modular', 50)->nullable()->comment('模块');
            $table->timestampsTz();
            $table->comment('管理员操作日志');
        });
    }

    /**
     * Reverse the migrations.
     */
    public function down(): void
    {
        Schema::dropIfExists('menu');
        Schema::dropIfExists('permission');
        Schema::dropIfExists('role');
        Schema::dropIfExists('admin_log');
    }
}
